﻿using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;
using System.Text;

namespace SalaryChannel.Web
{
    [ServiceContract(Namespace = "http://salary.yangchao.ts.gs.chinamobile/")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WiredNetService : ServiceAuthRoot
    {
        [OperationContract]
        public List<string> GetAreaName(string city)
        {

            try
            {
                this.Authoricate();
                using (DataClassesQueryDataContext context = new DataClassesQueryDataContext(this.SqlString))
                {
                    var query = from q in context.CM_Region
                                where q.CityCode == city
                                orderby q.CountyCode
                                select new { id = q.CountyCode, name = q.CountyName };
                    if (query.Any())
                    {
                        List<string> list = new List<string>(query.Count());
                        
                        foreach(var a in query)
                        {
                            list.Add( "[" + a.id + "]" + a.name);
                        }
                        return list;
                    }
                    return new List<string>();
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("WiredNetService.GetAreaName:" + ex.Message);
                return null;
            }
        }

        // 在此处添加更多操作并使用 [OperationContract] 标记它们
        [OperationContract]
        public bool RegisterNewDistrict(string AreaName, string DistrictName, string UserName,
            string UserTel, string UserAddress,string CreateUserID)
        {
            try
            {
                this.Authoricate();
                using (WiredNetDataClassesDataContext context = new WiredNetDataClassesDataContext(SqlString))
                {
                    context.Wired_Net_Register.InsertOnSubmit(new Wired_Net_Register
                     {
                          Area_Name =AreaName,
                          District_Name=DistrictName,
                          User_Address = UserAddress,
                          User_Name= UserName,
                          User_Tel=UserTel,
                           Reg_Time=DateTime.Now,
                          Reg_User_ID = CreateUserID
                     });
                    context.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("WiredNetService.RegisterNewDistrict:" + ex.Message);
                return false;
            }
        }

        [OperationContract]
        public List<Wired_Net_District> GetDistrictByName(string Name)
        {
            try
            {
                this.Authoricate();
                using (WiredNetDataClassesDataContext context = new WiredNetDataClassesDataContext(SqlString))
                {
                    var query = from q in context.Wired_Net_District
                                where q.District_Name.Contains(Name)
                                orderby q.Area_Name
                                select q;
                    if (query.Any())
                    {
                        return query.ToList();
                    }
                    return new List<Wired_Net_District>();
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("WiredNetService.GetDistrictByName:" + ex.Message);
                return null;
            }
        }


        [OperationContract]
        public List<Wired_Net_Register> GetRegDistrict(DateTime Start, DateTime End)
        {
            try
            {
                this.Authoricate();
                using (WiredNetDataClassesDataContext context = new WiredNetDataClassesDataContext(SqlString))
                {
                    var query = from q in context.Wired_Net_Register
                                where q.Reg_Time>=Start
                                where q.Reg_Time<= End
                                orderby q.Area_Name
                                select q;
                    if (query.Any())
                    {
                        return query.ToList();
                    }
                    return new List<Wired_Net_Register>();
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("WiredNetService.GetRegDistrict:" + ex.Message);
                return null;
            }
        }
        
        [OperationContract]
        public int  CreateNewDistrict(Wired_Net_District newDistrict)
        {
            try
            {
                this.Authoricate();
                using (WiredNetDataClassesDataContext context = new WiredNetDataClassesDataContext(SqlString))
                {
                    context.Wired_Net_District.InsertOnSubmit(newDistrict);
                    context.SubmitChanges();
                    return 0;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("WiredNetService.CreateNewDistrict:" + ex.Message);
                if (ex.Message.ToLower().Contains("index_wired_net_district_name"))
                {
                    return 1;
                }
                else
                {
                    return -1;
                }
            }
        }

        [OperationContract]
        public bool UpdateDistrict(Wired_Net_District District)
        {
            try
            {
                this.Authoricate();
                using (WiredNetDataClassesDataContext context = new WiredNetDataClassesDataContext(SqlString))
                {
                    var query = from q in context.Wired_Net_District
                                where q.ID==District.ID
                                select q;
                    if (query.Any())
                    {
                        query.First().Area_Name = District.Area_Name;
                        query.First().District_Name = District.District_Name;
                        query.First().District_Attribute = District.District_Attribute;
                        query.First().District_Cover_Users = District.District_Cover_Users;
                        query.First().Build_Status = District.Build_Status;
                        query.First().Expected_Completed_Date = District.Expected_Completed_Date;
                        query.First().Completed_Date = District.Completed_Date;
                        query.First().Update_User_ID = District.Update_User_ID;
                        query.First().Update_Time = District.Update_Time;
                        context.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("WiredNetService.UpdateDistrict:" + ex.Message);
                return false;
            }
        }

        [OperationContract]
        public bool DeleteDistrict(int ID)
        {
            try
            {
                this.Authoricate();
                using (WiredNetDataClassesDataContext context = new WiredNetDataClassesDataContext(SqlString))
                {
                    var query = from q in context.Wired_Net_District
                                where q.ID == ID
                                select q;
                    if (query.Any())
                    {
                        context.Wired_Net_District.DeleteOnSubmit(query.First());
                        context.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("WiredNetService.DeleteDistrict:" + ex.Message);
                return false;
            }
        }

        [OperationContract]
        public List<VW_Wlan_Hot> GetWlanHot(string hotString,int PageSize, int PageIndex)
        {
            try
            {
                this.Authoricate();
                using (WiredNetDataClassesDataContext context = new WiredNetDataClassesDataContext(SqlString))
                {
                    var query = from q in context.VW_Wlan_Hot
                                where q.Hot.Contains(hotString)
                                select q;
                    if (query.Any())
                    {
                        int count = query.Count();
                        int start = (PageIndex-1) * PageSize;
                       
                        if (start >= count)
                        {
                            return new List<VW_Wlan_Hot>();
                        }
                        int length = (count - start) >= PageSize ? PageSize : (count - start);
                        return query.Skip(start).Take(length).ToList();
                    }
                    return new List<VW_Wlan_Hot>();
                }
            }
            catch (Exception ex)
            {
                LogManager.Write("WiredNetService.GetWlanHot:" + ex.Message);
                return null;
            }
        }

        [OperationContract]
        public int UpdateWlanHot(DC_Wlan_Hot hot)
        {
            try
            {
                this.Authoricate();
                using (WiredNetDataClassesDataContext context = new WiredNetDataClassesDataContext(SqlString))
                {
                    if (hot.ID == -1)
                    {
                        //new
                        context.DC_Wlan_Hot.InsertOnSubmit(
                            new DC_Wlan_Hot
                            {
                                Area_Code = hot.Area_Code,
                                Hot = hot.Hot,
                                Remark = hot.Remark,
                                Create_Date = hot.Create_Date,
                                Create_ID = hot.Create_ID
                            });
                        context.SubmitChanges();
                        return 0;
                    }
                    else
                    {

                        var query = from q in context.DC_Wlan_Hot
                                    where q.ID == hot.ID
                                    select q;
                        if (query.Any())
                        {
                            DC_Wlan_Hot tem = query.First();
                            tem.Area_Code = hot.Area_Code;
                            tem.Hot = hot.Hot;
                            tem.Remark = hot.Remark;
                            context.SubmitChanges();
                            return 0;
                        }
                        return 1;//更新的热点不存在
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message.ToLower().Contains("ind_dc_wlan_hot"))
                {
                    return 2;//新增的热点已存在
                }
                LogManager.Write("WiredNetService.UpdateWlanHot:" + ex.Message);
                return -1;
            }
        }
    }
}
